package com.blockmeta.bbs.immodule.adapter;

import androidx.annotation.Nullable;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.net.callback.BaseResponseCallBack;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.TimeUtil;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.beaninterface.IJoinCircleRequestEntry;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.dao.IMUserEntity;
import com.blockmeta.bbs.immodule.utils.IMUtils;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author AirThor
 * @date 2019/4/4.
 */
public class JoinCircleRequestAdapter extends BaseAdapter<IJoinCircleRequestEntry, BaseViewHolder> {

    public JoinCircleRequestAdapter(int layoutResId, @Nullable List<IJoinCircleRequestEntry> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, IJoinCircleRequestEntry item) {
        helper.addOnClickListener(R.id.btn_pass);
        helper.addOnClickListener(R.id.btn_refuse);
        helper.addOnClickListener(R.id.btn_delete);
        helper.setGone(R.id.btn_pass, !item.isCompleted());
        helper.setGone(R.id.btn_refuse, !item.isCompleted());
        helper.setGone(R.id.tv_result, item.isCompleted());
        helper.setText(R.id.tv_result, item.isPassed() ? "已通过" : "已拒绝");

        CircleImageView avatar = helper.getView(R.id.contacts_avatar);
        //name 和头像 去数据库里面查找
        qrUserData(item, avatar, (TextView) helper.getView(R.id.contacts_name), (TextView) helper.getView(R.id.contacts_grade), (TextView) helper.getView(R.id.contacts_registration_time));
        helper.setText(R.id.tv_reason, item.getRequestMsg());
    }


    /**
     * 先查找数据库，去Friends里面找，没有再进行网络请求
     *
     * @param item
     * @param imageView
     * @param name
     */
    private void qrUserData(final IJoinCircleRequestEntry item, final CircleImageView imageView, final TextView name, final TextView grade, final TextView registerTime) {
        IMApplicationContext.getUserDomainService().getUser(item.getFromId(), false, new BaseResponseCallBack<String, IMUserEntity>() {
            @Override
            public void fail(String msg) {
            }

            @Override
            public void success(IMUserEntity IMUserEntity) {
                LoadPicUtil.loadWithPicasso(imageView, IMUserEntity.getAvatarPath());
                name.setText(IMUserEntity.getName());
                grade.setText(IMUtils.UserLevel.getUserLevel(IMUserEntity.getUserLevel()).getValue());
                registerTime.setText("加入 " + TimeUtil.getUserRegisterAge(IMUserEntity.getDate()));
            }
        });
    }
}
